package com.cosina.game.crystallight.ui.menu;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.EditText;

import com.cosina.game.crystallight.CrystalLightActivity;
import com.cosina.game.crystallight.MediaPlayerAdapter;
import com.cosina.game.crystallight.dao.ProfileDAOFacade;
import com.cosina.game.crystallight.model.profile.Profile;
import com.cosina.game.crystallight.model.profile.ProfileManager;
import com.cosina.game.crystallight.ui.ResourceManager;
import com.cosina.game.crystallight.ui.ResourceReference;
import com.cosina.game.crystallight.ui.menu.transform.ClickRotateTransform;
import com.cosina.game.crystallight.ui.menu.transform.FlyTransform;
import com.cosina.game.crystallight.ui.menu.transform.TransformSuite;
import com.cosina.game.crystallight.ui.menu.util.ViewPart;
import com.cosina.game.crystallight.util.DialogUtil;
import com.cosina.game.crystallight.util.World;

public class ProfileMenuView extends CompositeView {
	
	final int defaulStartX = 5;
	final int defaultStartY = 95;
	private CrystalLightActivity activity;
	public ProfileMenuView(CrystalLightActivity activity) {
		super(activity);
		this.activity = activity;
		Bitmap textFieldBitmap = ResourceManager.get(ResourceReference.IMG_MENU_TEXT_FIELD);
		Bitmap emptyFieldBitmap = ResourceManager.get(ResourceReference.IMG_MENU_EMPTY_FIELD);
		Bitmap delProfileBitmap = ResourceManager.get(ResourceReference.IMG_MENU_PROFILE_DELETE);
		Bitmap goMainMenuBitmap = ResourceManager.get(ResourceReference.IMG_MENU_BUT_MAIN_MENU);
		
		String[] profileNames = ProfileManager.getAllName();
		
		for(int index=0; index<profileNames.length; index++ ){
			boolean isEmpty = profileNames[index].equals(ProfileManager.EMPTY_PROFILE);
			Profile currentProfile = ProfileManager.getCurrentProfile();
			if(isEmpty){
				FlyTransform ft = new FlyTransform();
				ft.setFrom(World.LEFT);
				ft.delayCount += 5 * index;
				
				TransformSuite ts = TransformSuite.create(ft,new ClickRotateTransform());
				
				
				Point leftTop = new Point(defaulStartX,defaultStartY + index * 47);
				ViewPart btn = new ViewPart(index, this, emptyFieldBitmap, leftTop,ts);
				btn.text = ProfileManager.EMPTY_PROFILE;
				viewParts.add(btn);
			
			}else{
				{
					FlyTransform ft = new FlyTransform();
					ft.setFrom(World.LEFT);
					ft.delayCount += 5 * index;
					
					TransformSuite ts = TransformSuite.create(ft,new ClickRotateTransform());
					
					Point leftTop = new Point(defaulStartX,defaultStartY + index * 47);
					ViewPart btn = new ViewPart(index, this, textFieldBitmap, leftTop,ts);
					btn.text = profileNames[index];
					if(null != currentProfile && currentProfile.name.equals(btn.text)){
						btn.textColor = Color.YELLOW;
					}
					viewParts.add(btn);
				}
				{
					Point leftTop = new Point(defaulStartX + textFieldBitmap.getWidth()-3,defaultStartY + index * 47+ 6);
					ViewPart btn = new ViewPart(index*10, this, delProfileBitmap, leftTop);
					viewParts.add(btn);
				}
			}
		}
		
		{
			Point leftTop = new Point(105, 420);			
			ViewPart viewpart = new ViewPart(7,this, goMainMenuBitmap, leftTop);
			viewParts.add(viewpart);
		}
	}
	

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
	
	@Override
	public void onButtonClicked(int id) {
		MediaPlayerAdapter.menuBtnclick();
		switch (id) {
			case 7:
			activity.doShowMenu();
			return;
			default:
				String[] profileNames = ProfileManager.getAllName();
				if(id >= 10){
					showDeleteDialog(this.getContext(),profileNames[id/10],id/10);
					return ;
				}else{
					if(ProfileManager.EMPTY_PROFILE.equals(profileNames[id])){
						showInput(id,this.getContext());
						return ;
					}else{
						showUseThisProfile(this.getContext(), id);
					}
				}
		}
	}
	
	private void showDeleteDialog(final Context context,final String profileName,final int index) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle("Crystallight");
		builder.setMessage("Delete profile " + profileName + "?");
		builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ProfileDAOFacade.removeProfileName(index);
				((CrystalLightActivity)context).doShowProfileMenu();
			}
		});
		builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}
	
	
	private void showUseThisProfile(final Context context,final int index) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle("Crystallight");
		builder.setMessage("Play with this profile ?");
		builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			//;
			//
			@Override
			public void onClick(DialogInterface dialog, int which) {
				ProfileManager.setCurrentProfile(index);
				((CrystalLightActivity)context).doShowMap();
			}
		});
		builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		this.invalidate();
	}
	
	public static void showInput(final int index,final Context context){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		final EditText et = new EditText(context);
		{
			MarginLayoutParams lp = new MarginLayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
			lp.setMargins(20, 0, 20, 0);
			et.setLayoutParams(lp);
		}
		builder.setView(et);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle("Crystallight");
		builder.setMessage("Enter profile name");
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			//;
			//
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String textValue = et.getText().toString();
				if(null == textValue || textValue.trim().equals("") || textValue.trim().equals(ProfileManager.EMPTY_PROFILE)){
					DialogUtil.showAlert(context,"You can't use profile with empty name!");
					return ;
				}
				if(textValue.getBytes().length != textValue.length()){
					DialogUtil.showAlert(context,"You can't use profile with name!" + textValue);
					return ;
				}
				for(String each : ProfileDAOFacade.getProfileNames()){
					if(each.equals(textValue.trim())){
						DialogUtil.showAlert(context,"Profile with name " + each + " already existes");
						return ;
					}
				}
				ProfileDAOFacade.saveNewProfileName(textValue,index);
				ProfileManager.setCurrentProfile(index);
				((CrystalLightActivity)context).doShowProfileMenu();
			}
		});
		builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}
}
